<HTML>
<HEAD>
<!--
   Copyright 200-2004 The Apache Software Foundation
 
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
 
       http://www.apache.org/licenses/LICENSE-2.0
 
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->
<TITLE>Apache JServ Protocol version 1.3</TITLE>
</HEAD>

<BODY BGCOLOR="#FFFFFF">
<H1 ALIGN=CENTER>Apache JServ Protocol version 1.3</H1>
<P>

<DIV ALIGN=CENTER>
Dan Milstein, <A HREF="mailto:danmil@shore.net">danmil@shore.net</A>, December 2000.
</DIV>

<P> This describes the Apache JServ Protocol version 1.3 (hereafter
<B>ajp13</B>).  There is, apparently, no current documentation of how the
protocol works.  This document is an attempt to remedy that, in order to
make life easier for maintainers of mod_jk, and for anyone who wants to
port the protocol somewhere (into jakarta 4.x, for example).

<P><B>Who Am I?</B>

<BR>I am not one of the designers of this protocol -- I believe that Gal
Shachor was the original designer.  Everything in this document is derived
from the actual implementation I found in the tomcat 3.x code.  I hope it
is useful, but I can't make any grand claims to perfect accuracy.  I also
don't know why certain design decisions were made.  Where I was able, I've
offered some possible justifications for certain choices, but those are
only my guesses.  In general, the C code which Shachor wrote is very clean
and comprehensible (if almost totally undocumented).  I've cleaned up the
Java code, and I think it's reasonably readable.

<P>
<B>Design Goals</B>

<BR> According to email from Gal Shachor to the jakarta-dev mailing list,
the original goals of <B>mod_jk</B> (and thus <B>ajp13</B>) were to extend
<B>mod_jserv</B> and <B>ajp12</B> by (I am only including the goals which
relate to communication between the web server and the servlet container):

<OL>
  <LI> Increasing performance (speed, specifically).<P>

  <LI> Adding support for SSL, so that <CODE>isSecure()</CODE> and
       <CODE>geScheme()</CODE> will function correctly within the servlet
       container.  The client certificates and cipher suite will be
       available to servlets as request attributes.<P>

</OL>

<P>
<B>Overview</B>

<BR> The <B>ajp13</B> protocol is packet-oriented.  A binary format was
presumably chosen over the more readable plain text for reasons of
performance.  The web server communicates with the servlet container over
TCP connections.  To cut down on the expensive process of socket creation,
the web server will attempt to maintain persistent TCP connections to the
servlet container, and to reuse a connection for multiple request/response
cycles.

<P> Once a connection is assigned to a particular request, it will not be
used for any others until the request-handling cycle has terminated.  In
other words, requests are not multiplexed over connections.  This makes
for much simpler code at either end of the connection, although it does
cause more connections to be open at once.

<P> Once the web server has opened a connection to the servlet container,
the connection can be in one of the following states:

<UL>
  <LI> Idle <BR> No request is being handled over this connection. <P>
  <LI> Assigned <BR> The connecton is handling a specific request.
</UL>

<P> Once a connection is assigned to handle a particular request, the basic
request informaton (e.g. HTTP headers, etc) is sent over the connection in
a highly condensed form (e.g. common strings are encoded as integers).
Details of that format are below in Request Packet Structure. If there is a
body to the request (content-length &gt; 0), that is sent in a separate
packet immediately after.

<P> At this point, the servlet container is presumably ready to start
processing the request.  As it does so, it can send the
following messages back to the web server:

<UL>
  <LI>SEND_HEADERS <BR>Send a set of headers back to the browser.<P>

  <LI>SEND_BODY_CHUNK <BR>Send a chunk of body data back to the browser.<P>

  <LI>GET_BODY_CHUNK <BR>Get further data from the request if it hasn't all
  been transferred yet.  This is necessary because the packets have a fixed
  maximum size and arbitrary amounts of data can be included the body of a
  request (for uploaded files, for example).  (Note: this is unrelated to
  HTTP chunked tranfer).<P>

  <LI>END_RESPONSE <BR> Finish the request-handling cycle.
</UL>

Each message is accompanied by a differently formatted packet of data.  See
Response Packet Structures below for details.

<BR>
<BR>
<HR>
<BR>

<H2>Basic Packet Structure</H2>

<P>There is a bit of an XDR heritage to this protocol, but it differs in
lots of ways (no 4 byte alignment, for example).

<P>Byte order: I am not clear about the endian-ness of the individual
bytes.  I'm guessing the bytes are little-endian, because that's what XDR
specifies, and I'm guessing that sys/socket library is magically making
that so (on the C side).  If anyone with a better knowledge of socket calls
can step in, that would be great.

<P>There are four data types in the protocol: bytes, booleans, integers and
strings.

<DL>
  <DT><B>Byte</B></DT>
  <DD>A single byte.<P></DD>

  <DT><B>Boolean</B></DT>
  <DD>A single byte, 1 = true, 0 = false.  Using other non-zero values as
  true (i.e. C-style) may work in some places, but it won't in
  others.<P></DD>
  
  <DT><B>Integer</B></DT>
  <DD>A number in the range of 0 to 2^16 (32768).  Stored in 2 bytes with
  the high-order byte first.<P></DD>

  <DT><B>String</B></DT>
  <DD>A variable-sized string (length bounded by 2^16). Encoded with the
  length packed into two bytes first, followed by the string (including the
  terminating '\0').  Note that the encoded length does <B>not</B> include
  the trailing '\0' -- it is like <CODE>strlen</CODE>.  This is a touch
  confusing on the Java side, which is littered with odd autoincrement
  statements to skip over these terminators.  I believe the reason this was
  done was to allow the C code to be extra efficient when reading strings
  which the servlet container is sending back -- with the terminating \0
  character, the C code can pass around references into a single buffer,
  without copying.  If the \0 was missing, the C code would have to copy
  things out in order to get its notion of a string.
</DL>

<P><B>Packet Size</B> <BR> According to much of the code, the max packet
size is 8 * 1024 bytes (8K).  The actual length of the packet is encoded in the
header.

<P><B>Packet Headers</B>

<BR> Packets sent from the server to the container begin with
<CDOE>0x1234</CODE>.  Packets sent from the container to the server begin
with <CODE>AB</CODE> (that's the ASCII code for A followed by the ASCII
code for B).  After those first two bytes, there is an integer (encoded as
above) with the length of the payload.  Although this might suggest that
the maximum payload could be as large as 2^16, in fact, the code sets the
maximum to be 8K.

<P>

<TABLE BORDER=1 CELLPADDING=2 CELLSPACING=0 BGCOLOR="#FFFFFF">
  <TR>
    <TD COLSPAN=6><B>Packet Format (Server-&gt;Container)</B></TD>
  </TR>

  <TR>
    <TD BGCOLOR="#C0C0C0">Byte</TD>
    <TD ALIGN=CENTER>0</TD>
    <TD ALIGN=CENTER>1</TD>
    <TD ALIGN=CENTER>2</TD>
    <TD ALIGN=CENTER>3</TD>
    <TD ALIGN=CENTER WIDTH=100>4...(n+3)</TD>
  </TR>

  <TR>
    <TD BGCOLOR="#C0C0C0">Contents</TD>
    <TD BGCOLOR="#FF9999">0x12</TD>
    <TD BGCOLOR="#FF9999">0x34</TD>
    <TD COLSPAN=2 BGCOLOR="#CCFFCC">Data Length (n)</TD>
    <TD BGCOLOR="#CCFFFF" ALIGN=CENTER>Data</TD>
  </TR>
</TABLE>

<P>

<TABLE BORDER=1 CELLPADDING=2 CELLSPACING=0 BGCOLOR="#FFFFFF">
  <TR>
    <TD COLSPAN=6><B>Packet Format (Container-&gt;Server)</B></TD>
  </TR>

  <TR>
    <TD BGCOLOR="#C0C0C0">Byte</TD>
    <TD ALIGN=CENTER>0</TD>
    <TD ALIGN=CENTER>1</TD>
    <TD ALIGN=CENTER>2</TD>
    <TD ALIGN=CENTER>3</TD>
    <TD ALIGN=CENTER WIDTH=100>4...(n+3)</TD>
  </TR>

  <TR>
    <TD BGCOLOR="#C0C0C0">Contents</TD>
    <TD BGCOLOR="#FF9999">A</TD>
    <TD BGCOLOR="#FF9999">B</TD>
    <TD COLSPAN=2 BGCOLOR="#CCFFCC">Data Length (n)</TD>
    <TD BGCOLOR="#CCFFFF" ALIGN=CENTER>Data</TD>
  </TR>
</TABLE>


<A NAME="prefix-codes"><P></A> For most packets, the first byte of the
payload encodes the type of message.  The exception is for request body
packets sent from the server to the container -- they are sent with a
standard packet header (0x1234 and then length of the packet), but without
any prefix code after that (this seems like a mistake to me).  The web
server can send the following messages to the servlet container:

<P>
<TABLE BORDER=2 CELLSPACING=0 CELLPADDING=5>
  <TR>
    <TD ALIGN=CENTER>Code</TD>
    <TD>Type of Packet</TD>
    <TD>Meaning</TD>
  </TR>
  <TR>
    <TD ALIGN=CENTER>2</TD>
    <TD>Forward Request</TD>
    <TD>Begin the request-processing cycle with the following data</TD>
  </TR>
  <TR>
    <TD ALIGN=CENTER>7</TD>
    <TD>Shutdown</TD>
    <TD>The web server asks the container to shut itself down.
  </TR>
  <TR>
    <TD ALIGN=CENTER>8</TD>
    <TD>Ping</TD>
    <TD>The web server asks the container to quickly respond with a Pong.
  </TR>
</TABLE>

<P>The servlet container can send the following types of messages to the web
server:
<P>
<TABLE BORDER=2 CELLSPACING=0 CELLPADDING=5>
  <TR>
    <TD>Code</TD>
    <TD>Type of Packet</TD>
    <TD>Meaning</TD>
  </TR>
    <TR>
    <TD ALIGN=CENTER>3</TD>
    <TD>Send Body Chunk</TD>
    <TD>Send a chunk of the body from the servlet container to the web
    server (and presumably, onto the browser). </TD>
  </TR>
  </TR>
    <TR>
    <TD ALIGN=CENTER>4</TD>
    <TD>Send Headers</TD>
    <TD>Send the response headers from the servlet container to the web
    server (and presumably, onto the browser).</TD>
  </TR>
  </TR>
    <TR>
    <TD ALIGN=CENTER>5</TD>
    <TD>End Response</TD>
    <TD>Marks the end of the response (and thus the request-handling cycle).</TD>
  </TR>
  </TR>
    <TR>
    <TD ALIGN=CENTER>6</TD>
    <TD>Get Body Chunk</TD>
    <TD>Get further data from the request if it hasn't all been transferred
    yet.</TD>
  </TR>
  </TR>
    <TR>
    <TD ALIGN=CENTER>9</TD>
    <TD>Pong</TD>
    <TD>Reply to a Ping request.</TD>
  </TR>
</TABLE>

<P>Each of the above messages has a different internal structure, detailed below.

<BR>
<BR>
<HR>
<BR>

<H2>Request Packet Structure</H2>

<P>For messages from the server to the container of type "Forward Request":

<PRE>
AJP13_FORWARD_REQUEST :=
    prefix_code      2
    method           (byte)
    protocol         (string)
    req_uri          (string)
    remote_addr      (string)
    remote_host      (string)
    server_name      (string)
    server_port      (integer)
    is_ssl           (boolean)
    num_headers      (integer)
    request_headers *(req_header_name req_header_value)

    ?context       (byte string)
    ?servlet_path  (byte string)
    ?remote_user   (byte string)
    ?auth_type     (byte string)
    ?query_string  (byte string)
    ?jvm_route     (byte string)
    ?ssl_cert      (byte string)
    ?ssl_cipher    (byte string)
    ?ssl_session   (byte string)

    ?attributes   *(attribute_name attribute_value)
    request_terminator (byte)

req_header_name := 
    sc_req_header_name | (string)  [see below for how this is parsed]

sc_req_header_name := 0xA0 (byte)

req_header_value := (string)

attribute_name := (string)

attribute_value := (string)

request_terminator := 0xFF
</PRE>

Not that the all-important header is "content-length', because it
determines whether or not the container looks for another packet
immediately.

<P>Details of above

<DL>
  <DT><B>request_prefix</B></DT>
  <DD> For all requests, this will be 2.  See
  above for details on other <A HREF="#prefix-codes">prefix codes</A>.<P>
  </DD>

  <DT><B>method</B></DT>
  <DD>The HTTP method, encoded as a single byte:
  <P>
  <PRE>
OPTIONS      1
GET          2
HEAD         3
POST         4
PUT          5
DELETE       6
TRACE        7
PROPFIND     8
PROPPATCH    9
MKCOL       10
COPY        11
MOVE        12
LOCK        13
UNLOCK      14
ACL         15
REPORT      16
VERSION-CONTROL 17
CHECKIN     18
CHECKOUT    19
UNCHECKOUT  20
SEARCH      21
  </PRE>
  <P>
  </DD>

  <DT><B>protocol, req_uri, remote_addr, remote_host, server_name, server_port, is_ssl</B></DT>
  <DD>
  These are all fairly self-explanatory.  Each of these is required, and
  will be sent for every request.<P>
  </DD>

  <DT><B>Headers</B></DT>
  <DD>First, the number of headers is encoded.  Then, a series of header
  name / value pairs follows.  Common header names are encoded as integers,
  to save space.  If the header name is not in the list of basic headers,
  it is encoded normally (as a string, with prefixed length).  The list of
  common headers and their codes is as follows (all are case-sensitive):
  <PRE>
accept               0xA001
accept-charset       0xA002
accept-encoding      0xA003
accept-language      0xA004
authorization        0xA005
connection           0xA006
content-type         0xA007
content-length       0xA008
cookie               0xA009    
cookie2              0xA00A
host                 0xA00B
pragma               0xA00C
referer              0xA00D
user-agent           0xA00E
  </PRE>

  The Java code that reads this grabs the first two-byte integer, and, if
  <A NAME="header_encoding">it sees an '0xA0'</A> in the most significant
  byte, it uses the integer in the second byte as an index into an array of
  header names.  If the first byte is not '0xA0', it assumes that the
  two-byte integer is the length of a string, which is then read in.<P>

  This works on the assumption that no header names will have length
  greater than 0x9999 (==0xA000 - 1), which is perfectly reasonable, though
  somewhat arbitrary. (If you, like me, started to think about the cookie
  spec here, and about how long headers can get, fear not -- this limit is
  on header <B>names</B> not header <B>values</B>.  It seems unlikely that
  unmanageably huge header names will be showing up in the HTTP spec any time
  soon). <P>

  <B>Note:</B> The <CODE>content-length</CODE> header is extremely
  important.  If it is present and non-zero, the container assumes that
  the request has a body (a POST request, for example), and immediately
  reads a separate packet off the input stream to get that body.<P>

  <DT><B>Optional Information</B></DT>

  <DD>The list of attributes prefixed with a <CODE>?</CODE>
  (e.g. <CODE>?context</CODE>) are all optional.  For each, there is a
  single byte code to indicate the type of attribute, and then a string to
  give its value.  They can be sent in any order (thogh the C code always
  sends them in the order listed below).  A special terminating code is
  sent to signal the end of the list of optional attributes. The list of
  byte codes is:
  
  <PRE>
context            1 [Not currently implemented]
servlet_path       2 [Not currently implemented]
remote_user        3
auth_type          4
query_string       5
jvm_route          6
ssl_cert           7
ssl_cipher         8
ssl_session        9

req_attribute      10 

terminator         0xFF
   </PRE>

  The <CODE>context</CODE> and <CODE>servlet_path</CODE> are not currently
  set by the C code, and most of the Java code completely ignores whatever
  is sent over for those fields (and some of it will actually break if a
  string is sent along after one of those codes).  I don't know if this is
  a bug or an unimplemented feature or just vestigial code, but it's
  missing from both sides of the connection.<P>
  
  The <CODE>remote_user</CODE> and <CODE>auth_type</CODE> presumably refer
  to HTTP-level authentication, and communicate the remote user's username
  and the type of authentication used to establish their identity (e.g. Basic,
  Digest).  I'm not clear on why the password isn't also sent, but I don't
  know HTTP authentication inside and out. <P>

  The <CODE>query_string</CODE>, <CODE>ssl_cert</CODE>,
  <CODE>ssl_cipher</CODE>, and <CODE>ssl_session</CODE> refer to the
  corresponding pieces of HTTP and HTTPS.<P>

  The <CODE>jvm_route</CODE>, as I understand it, is used to support sticky
  sessions -- associating a user's sesson with a particular Tomcat instance
  in the presence of multiple, load-balancing servers.  I don't know the
  details. <P>

  Beyond this list of basic attributes, any number of other attributes can
  be sent via the <CODE>req_attribute</CODE> code (10).  A pair of strings
  to represent the attribute name and value are sent immediately after each
  instance of that code.  Environment values are passed in via this method. <P>

  Finally, after all the attributes have been sent, the attribute terminator,
  0xFF, is sent.  This signals both the end of the list of attributes, and
  also then end of the Request Packets as a whole.<P>
  </DD>
</DL>

The server can also send a <code>shutdown</CODE> packet.  To ensure some
basic security, the container will only actually do the shutdown if the
request comes from the same machine on which it's hosted. <P>

<HR>
<BR>

<H2>Response Packet Structures</H2>

<P>For messages which the container can send back to the server.

<PRE>
AJP13_SEND_BODY_CHUNK := 
  prefix_code   3
  chunk_length  (integer)
  chunk        *(byte)


AJP13_SEND_HEADERS :=
  prefix_code       4
  http_status_code  (integer)
  http_status_msg   (string)
  num_headers       (integer)
  response_headers *(res_header_name header_value)

res_header_name := 
    sc_res_header_name | (string)   [see below for how this is parsed]

sc_res_header_name := 0xA0 (byte)

header_value := (string)

AJP13_END_RESPONSE :=
  prefix_code       5
  reuse             (boolean)


AJP13_GET_BODY_CHUNK :=
  prefix_code       6
  requested_length  (integer)
</PRE>

Details:

<DL>
  <DT><B>Send Body Chunk</B></DT>
  <DD>The chunk is basically binary data, and is sent directly back to the browser.<P>
  </DD>

  <DT><B>Send Headers</B></DT>
  <DD>The status code and message are the usual HTTP things (e.g. "200" and "OK").
  The response header names are encoded the same way the request header names are.
  See <A HREF="#header_encoding">above</A> for details about how the the
  codes are distinguished from the strings.  The codes for common headers are:

<PRE>
Content-Type         0xA001
Content-Language     0xA002
Content-Length       0xA003 
Date                 0xA004
Last-Modified        0xA005
Location             0xA006
Set-Cookie           0xA007
Set-Cookie2          0xA008
Servlet-Engine       0xA009
Status               0xA00A
WWW-Authenticate     0xA00B
</PRE>

  After the code or the string header name, the header value is immediately
  encoded.<P>

  <DT><B>End Response</B></DT>
  <DD> Signals the end of this request-handling cycle.  If the
  <CODE>reuse</CODE> flag is true (==1), this TCP connection can now be used to
  handle new incoming requests.  If <CODE>reuse</CODE> is false (anything
  other than 1 in the actual C code), the connection should be closed.<P>

  <DT><B>Get Body Chunk</B></DT>
  <DD> The container asks for more data from the request (if the body was
  too large to fit in the first packet sent over).  The server will send a
  body packet back with an amount of data which is the minimum of the
  <CODE>request_length</CODE>, the maximum send body size (XXX), and the
  number of bytes actually left to send from the request body.<P>

  If there is no more data in the body (i.e. the servlet container is
  trying to read past the end of the body), the server will send back an
  "empty" packet, whch is a body packet with a payload length of 0.
  
</DL>

<P>

<HR>

<H2>Questions I Have</H2>

<P> What happens if the request headers &gt; max packet size?  There is no
provision to send a second packet of request headers in case there are more
than 8K (I think this is correctly handled for response headers, though I'm
not certain).  I don't know if there is a way to get more than 8K worth of
data into that initial set of request headers, but I'll bet there is
(combine long cookies with long ssl information and a lot of environment
variables, and you should hit 8K easily).  I think the connector would just
fail before trying to send any headers in this case, but I'm not certain.

<P> What about authentication?  There doesn't seem to be any authentication
of the connection between the web server and the container.  This strikes
me as potentially dangerous.


</BODY>
</HTML>
